import "./theme.less";

const themes: any = {};
export const themesName: any = {};
const _themes = import.meta.glob("./model/*.ts", { eager: true });
Object.keys(_themes).forEach((key) => {
  const themeName = key.replace(/.*\//, "").replace(/\.ts$/, "");
  const themeConfig = _themes[key]?.default;
  themes[themeName] = themeConfig;
  const uppercaseName: string = themeName.toUpperCase();
  themesName[uppercaseName] = themeName;
});

export const changeTheme = (theme?: string) => {
  const name = theme || localStorage.getItem("theme-default");
  if (name === "default") {
    document.documentElement.setAttribute("style", "");
    localStorage.setItem("theme-default", "");
    return false;
  }
  if (name && Object.values(themesName).includes(name)) {
    localStorage.setItem("theme-default", name);
    Object.keys(themes[name]).forEach((key) => {
      document.documentElement.style.setProperty(key, themes[name][key]);
    });
  }
};
